/**
 * 添加或者修改页面
 */
var TaoRecommendGoodsInfoDlg = {
    data: {
        goodId: "",
        pic: "",
        goodType: "",
        createTime: "",
        status: ""
    }
};

layui.use(['form', 'admin', 'ax','upload','formSelects'], function () {
    var $ = layui.jquery;
    var $ax = layui.ax;
    var form = layui.form;
    var admin = layui.admin;
    var upload = layui.upload;
    var formSelects = layui.formSelects;

    //让当前iframe弹层高度适应
    admin.iframeAuto();

    //表单提交事件
    form.on('submit(btnSubmit)', function (data) {
        var ajax = new $ax(Feng.ctxPath + "/taoRecommendGoods/addItem", function (data) {
            Feng.success("添加成功！");
            window.location.href = Feng.ctxPath + '/taoRecommendGoods'
        }, function (data) {
            Feng.error("添加失败！" + data.responseJSON.message)
        });
        ajax.set(data.field);
        ajax.start();

        return false;
    });

    //返回按钮
    $("#backupPage").click(function () {
        window.location.href = Feng.ctxPath + '/taoRecommendGoods'
    });

    /**
     * 上传图片
     */
    upload.render({
        elem: '#picBtn'
        , url: Feng.ctxPath + '/taoRecommendGoods/multipleGraph'
        , before: function (obj) {
            obj.preview(function (index, file, result) {
                $('#img1').attr('src', result);
            });
        }
        , done: function (res) {
            $("#pictureInputHidden").val(res.data.fileId);
            Feng.success(res.message);
        }
        , error: function () {
            Feng.error("上传图片失败！");
        }
    });

    //分类选择
    $.ajax({
        url: Feng.ctxPath + "/classification/getMenu",
        success: function (data) {
            var html = '<option value="" >' + "请选择" + '</option>';
            // 加载数据
            for (var i = 0; i < data.data.length; i++) {
                html += '<option value="' + data.data[i].id + '">'
                    + data.data[i].name + '</option>'
            }
            $("#type").append(html);
            form.render();
        }
    });

    //商品选择
    form.on('select(type)', function (data) {
        var keys = [];
        $.ajax({
            url: Feng.ctxPath + "/classification/getGoodsById?id=" + data.value,
            success: function (data) {
                debugger;
                // 加载数据
                for (var i = 0; i < data.data.length; i++) {
                    var temp = {"name":data.data[i].fullname,"value":data.data[i].ids}
                    keys.push(temp);
                }
                formSelects.data('select1', 'local', {
                    arr: keys
                });
            }
        });
    });

});